﻿
Public Class WinccConfig
    Implements IConfig
    Private dts As String
    Private ctlg As String
    Private reqlogin As Boolean
    Private user As String
    Private pswd As String
    Private verrs As String


    Public Property Datasource As String
        Get
            Return dts
        End Get
        Set(ByVal value As String)
            dts = value
        End Set
    End Property

    Public Property Catalog As String
        Get
            Return ctlg
        End Get
        Set(ByVal value As String)
            ctlg = value
        End Set
    End Property

    Public Property RequireLogin As Boolean
        Get
            Return reqlogin
        End Get
        Set(ByVal value As Boolean)
            reqlogin = value
        End Set
    End Property

    Public Property LoginName As String
        Get
            Return user
        End Get
        Set(ByVal value As String)
            user = value
        End Set
    End Property

    Public Property LoginPswd As String
        Get
            Return pswd
        End Get
        Set(ByVal value As String)
            pswd = value
        End Set
    End Property

    Public Property ValidationErrors As String Implements IConfig.validationerrors
        Get
            Return verrs
        End Get
        Set(ByVal value As String)
            verrs = value
        End Set
    End Property

    Public Function checkConfig() As Boolean Implements IConfig.checkConfig
        If (String.IsNullOrEmpty(dts)) Then
            ValidationErrors = "WinCC Datasource must be specified" + vbCrLf + "If the datasource is not specified or correct, the service will fail starting." + vbCrLf + "Do you want to continue?"
            Return False
        End If
        If (String.IsNullOrEmpty(ctlg)) Then
            ValidationErrors = "WinCC Catalog must be specified" + vbCrLf + "If the datasource is not specified or correct, the service will fail starting." + vbCrLf + "Do you want to continue?"
            Return False
        End If
        If (reqlogin) Then
            If (String.IsNullOrEmpty(user)) Then
                ValidationErrors = "Username must be specified if Require login is enable"
                Return False
            End If
        End If
        Return True
    End Function

    Public Function loadConfig() As Boolean Implements IConfig.loadConfig
        Datasource = My.Settings.wc_ds
        Catalog = My.Settings.wc_cat
        RequireLogin = My.Settings.wc_login
        LoginName = My.Settings.wc_user
        LoginPswd = My.Settings.wc_pswd
        AlarmView = My.Settings.wc_alarmview
        Return True
    End Function

    Public Function writeConfig() As Boolean Implements IConfig.writeConfig
        My.Settings.wc_cat = Catalog
        My.Settings.wc_ds = Datasource
        My.Settings.wc_login = RequireLogin
        My.Settings.wc_pswd = LoginPswd
        My.Settings.wc_user = LoginName
        My.Settings.wc_alarmview = AlarmView
        Return True
    End Function

    Sub New()
        loadConfig()
    End Sub
    Public Function getConnectionString() As String
        Dim cm As String = ""
        Dim cxb As New OleDb.OleDbConnectionStringBuilder
        cxb.DataSource = Datasource
        If (My.Settings.op_isdebug) Then
            cxb.Provider = SQLPROVIDER
            cxb.Add("Initial Catalog", Catalog)
            cxb.Add("Integrated Security", "SSPI")
            cxb.Add("Pooling", False)
        Else
            cxb.Provider = WINCCPROVIDER
            cxb.Add("Catalog", Catalog)
        End If
        cm = cxb.ToString
        Return cm
    End Function
    Public Function getTagConnectionString() As String
        Dim cm As String = ""
        Dim cxb As New OleDb.OleDbConnectionStringBuilder
        cxb.DataSource = Datasource

        cxb.Provider = SQLPROVIDER
        cxb.Add("Initial Catalog", Catalog)
        If (RequireLogin) Then
            cxb.Add("uid", LoginName)
            cxb.Add("pwd", LoginPswd)
        Else
            cxb.Add("Integrated Security", "SSPI")
        End If
        cm = cxb.ToString
        Return cm
    End Function
    Public Function getConnectionString(ByVal cat As String) As String
        Dim cm As String = "Provider=" + WINCCPROVIDER + ";"
        cm = cm + "Data Source=" + Datasource + ";"
        cm = cm + "Catalog=" + cat + ";"
        Return cm
    End Function
    Public Const WINCCPROVIDER As String = "WinCCOLEDBProvider.1"
    Public Const SQLPROVIDER As String = "SQLOLEDB"
    Public Const ARCHIVE = "WinCC Tag Loggin"
    Public Const ALARM = "WinCC Alarms Loggin"
    Public Const TAGID As String = "ValueId"
    Public Const TAGNAME As String = "ValueName"
    Public Const PROCTAGID As String = "ValueID"
    Public Const PROCTAGTIME As String = "TimeStamp"
    Public Const PROCTAGVAL As String = "RealValue"
    Private alview As String

    Public Property AlarmView As String
        Get
            Return alview
        End Get
        Set(ByVal value As String)
            alview = value
        End Set
    End Property


End Class
